package com.rentalcar.rental_car.service.impl;

import com.rentalcar.rental_car.dao.CustomerMapper;
import com.rentalcar.rental_car.entity.Customer;
import com.rentalcar.rental_car.entity.Result;
import com.rentalcar.rental_car.service.CustomerService;
import com.rentalcar.rental_car.utils.JwtUtils;
import com.rentalcar.rental_car.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    CustomerMapper customerMapper;

    @Override
    public List<Customer> getCustomerAll() {

        return customerMapper.getCustomerAll();
    }

    @Override
    public Customer selectByIdCard(String idCard) {

        return customerMapper.selectByIdCard(idCard);
    }

    @Override
    public List<Customer> selectByCname(Customer customer) {

        return customerMapper.selectByName(customer);
    }

    @Override
    public int update(Customer customer) {

        return customerMapper.update(customer);
    }

    @Override
    public Result loginCheck(Customer customer) {

        customer.setPassword(MD5.encoding(customer.getPassword()));
        Customer c = customerMapper.loginCheck(customer);

        if (c == null) {
            return Result.fail("账号密码错误");
        }
        HashMap<String, Object> data = new HashMap<>();
//        try {
//            HashMap<String, String> payload = new HashMap<>();
//            payload.put("cname", c.getCname());
//            String token = TokenUtils.getToken(payload);
//            c.setToken(token);
//            customerMapper.updateToken(c);
//            data.put("custKey",c.getCustKey());
//            data.put("cname", c.getCname());
//            data.put("address",c.getAddress());
//            data.put("phone",c.getPhone());
//            data.put("uname",c.getUname());
//            data.put("token", token);
//            return Result.success(data);
//        } catch (Exception e) {
//            return Result.fail("登录失败");
//        }
        try {
            data.put("custKey",c.getCustKey());
            data.put("cname",c.getCname());
            data.put("uname",c.getUname());
            data.put("address",c.getAddress());
            data.put("phone",c.getPhone());
            String jwt = JwtUtils.generateJwt(data);
            c.setToken(jwt);
            customerMapper.updateToken(c);
            return Result.success(c);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("登录失败");
        }
    }

    @Override
    public Result addCustomer(Customer customer) {

        List<Customer> customerList = customerMapper.selectByName(customer);
        if(customerList.size()>0)
            return Result.fail("账号已存在");
        if ("".equals(customer.getCname())||"".equals(customer.getPassword()))
            return Result.fail("账号密码不能为空");
        customer.setPassword(MD5.encoding(customer.getPassword()));
        customerMapper.addCustomer(customer);
        return Result.success();
    }

    @Override
    public Result logOut(Customer customer) {

        Customer c = customerMapper.selectByName(customer).get(1);
        c.setToken(null);
        if (customerMapper.updateToken(c)==1){
            return Result.success();
        }
        return Result.fail("退出失败");
    }

}
